package io.renren.common.config;

import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.http.HttpProtocol;
import com.qcloud.cos.region.Region;
import com.qcloud.cos.transfer.TransferManager;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author yun lang
 * @version 1.0
 * @description: TODO
 * @date 2024/11/5 16:00
 */
@Configuration
public class CosConfig {
    @Value("${tencent.cos.secretId}")
    private String secretId;

    @Value("${tencent.cos.secretKey}")
    private String secretKey;

    @Value("${tencent.cos.region}")
    private String region;

    @Bean
    public ExecutorService threadPool() {
        return Executors.newFixedThreadPool(32);
    }

    @Bean
    public COSClient cosClient() {
        // 1. 初始化用户身份信息（secretId, secretKey）
        COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);

        // 2. 设置 bucket 的地域
        Region regionObj = new Region(region);
        ClientConfig clientConfig = new ClientConfig(regionObj);

        // 这里建议设置使用 https 协议
        clientConfig.setHttpProtocol(HttpProtocol.https);

        // 3. 生成 cos 客户端
        return new COSClient(cred, clientConfig);
    }

    @Bean
    public TransferManager transferManager(COSClient cosClient, ExecutorService threadPool) {
        // 使用自定义的线程池创建 TransferManager
        return new TransferManager(cosClient, threadPool);
    }
}
